//
//  ViewController.m
//  03-UIgestureRecognize
//
//  Created by dllo on 16/7/18.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *yellowMan;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor orangeColor];
    // 1.亲拍
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:tap];
    // 2.长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [self.view addGestureRecognizer:longPress];
    // 长按2秒触发
//    longPress.minimumPressDuration = 2;
    // 最大移动距离,超过距离就失效默认是10
    longPress.allowableMovement = 100;
    // 3.清扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    [self.view addGestureRecognizer:swipe];
    // direction 可以触发多个方向,但是行为必须相同,如果多个行为,则创建多个清扫手势对象
    // 左右与上下有冲突
    // "|" 或者
    swipe.direction = UISwipeGestureRecognizerDirectionLeft |UISwipeGestureRecognizerDirectionRight;
    

    // swipe.view 此view 是被加手势的view
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"03.jpg"]];
    imageView.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 4, [UIScreen mainScreen].bounds.size.height / 4);
    [self.view addSubview:imageView];
    // 记住添加手势在imageView上
    [imageView addGestureRecognizer:swipe];
    imageView.userInteractionEnabled = YES;
    // 平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [imageView addGestureRecognizer:pan];
    // 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imageView addGestureRecognizer:pinch];
    //
    
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
}
- (void)panAction:(UIPanGestureRecognizer *)pan {
    if (pan.state == UIGestureRecognizerStateBegan) {
        // 平移开始
    } else if (pan.state == UIGestureRecognizerStateChanged) {
        // 根据point使得pan.view移动
        CGPoint point =[pan translationInView:pan.view];
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        // 移动的点置空(使得不会反弹)
        [pan setTranslation:CGPointZero inView:pan.view];
    } else if (pan.state == UIGestureRecognizerStateEnded) {
        pan.view.transform = CGAffineTransformIdentity;
    }
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {

    // 附在imageView上再强转
    UIImageView *imageView = (UIImageView *)swipe.view;
    [imageView setImage:[UIImage imageNamed:@"02.jpg"]];
    NSLog(@"清扫");
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    } else if (longPress.state == UIGestureRecognizerStateChanged) {
        NSLog(@"长按中");
    } else if (longPress.state == UIGestureRecognizerStateEnded) {
        NSLog(@"长按结束");
    }
}
- (void)tapAction:(UITapGestureRecognizer *)tap {
    NSLog(@"轻拍");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
